<html>
<head>
<script src="common/port.js"></script>
<script src="common/config.js"></script>
<script src="background.js"></script>

<script type = "text/javascript">

	function fixRegularExpressions()
	{
		for (var i=0; i < whitelist.length; i++)
		{	
			if (whitelist[i] == "^(http[s]?:\/\/[a-z0-9\._-]+\.|http[s]?:\/\/)google\.[a-z]+($|\/)")
			{
				whitelist[i] = "^(http[s]?:\\/\\/[a-z0-9\\._-]+\\.|http[s]?:\\/\\/)google\\.[a-z]+($|\\/)";
			}
			else if (whitelist[i] == "^(http[s]?:\/\/[a-z0-9\._-]+\.|http[s]?:\/\/)youtube\.[a-z]+($|\/)")
			{
				whitelist[i] = "^(http[s]?:\\/\\/[a-z0-9\\._-]+\\.|http[s]?:\\/\\/)youtube\\.[a-z]+($|\\/)";
			}
			else if (whitelist[i] == "^(http[s]?:\/\/[a-z0-9\._-]+\.|http[s]?:\/\/)pezcyclingnews\.[a-z]+($|\/)")
			{
				whitelist[i] = "^(http[s]?:\\/\\/[a-z0-9\\._-]+\\.|http[s]?:\\/\\/)pezcyclingnews\\.[a-z]+($|\\/)";
			}
		}
		config.set("whitelist", whitelist);
	}
	
	if (config.get('currVersion') < 200001006)
	{	
		var currVersion = config.get('currVersion');
		
		// We used incorrectly escaped regular expressions in versions previous to 1.2 so we are trying
		// to fix the default ones here	
		if (currVersion < 100200000)
		{
			fixRegularExpressions();
		}

		if (currVersion < 200000000)
		{
			// document.createEvents is often used legitimately, so we are now allowing it by default
			config.set("blockCreateEvents", false);
			
			// The option to show extended tooltips and stripping javascript from links was split so this is reset
			config.set("extendedTooltips", false);
		}
		
		if (currVersion < 200001006)
		{
			// Some users were having site compatiblity issues when blocking window targets so we are resetting it for everyone.
			config.set("blockWindowTargets", false);
		}		
		
		config.set('currVersion', 200001006);
		config.set('currDisplayVersion', "2.1.6");
	}
	
	config.set('globalAllowAll', false);
	config.set('tempList', new Array());
	const maxBlinks = 6;
	var lastAnimatedTab = null;	
	var animationTimer = null;
	
	if (SAFARI)
	{
		safari.application.addEventListener("validate", validateCommand, false);
		safari.application.addEventListener("command", performCommand, false);

		//safari.application.addEventListener("message", handleMessage, false);	
		for (var i = 0; i < safari.application.browserWindows.length; i++)
		{
			var currWindow = safari.application.browserWindows[i];
			for (var j = 0; j < currWindow.tabs.length; j++)
			{
				currWindow.tabs[j].url = currWindow.tabs[j].url;
			}			
		}		
	}
	else
	{
		chrome.tabs.onUpdated.addListener(function(tabid, changeinfo, tab) {
			// Do I really need to cancel the animation here?
			if (animationTimer && tab.id === lastAnimatedTab)
			{
				clearTimeout(animationTimer);
				lastAnimatedTab = null;
				animationTimer = null;				
			}
			if (config.get('showPageActionButton'))
			{				
				if (urlsGloballyAllowed || isAllowed(tab.url))
				{
					chrome.pageAction.setIcon({path: "IconAllowed.png", tabId: tab.id});
				}
				else
				{
					chrome.pageAction.setIcon({path: "IconForbidden.png", tabId: tab.id});
				}
				chrome.pageAction.show(tab.id);
			}
			else
			{
				chrome.pageAction.hide(tab.id);
			}
		});	
		
		
		chrome.windows.onCreated.addListener(
			function(wnd) {
				// valid values for type: normal, popup
				if (wnd.type === "popup" && config.get("globalAllowAll") !== true && config.get("closeAllPopUpWindows") === true)
					chrome.windows.remove(wnd.id);
				
			}
		);	
				
	}
		
</script>
</head>
</html>


